<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class fatair extends CI_Controller {
	
	public $user_data=array();
	public $WEB_SET;
	public $WEB_Language="zh";
	
	
	function __construct()
     {

     	parent::__construct();
		
		
		
     	//按当前语言载入语言文件
     	if($this->session->userdata("lang")=="")
     	{
			$this->lang->load('info',"zh");
		}else
		{
			$this->lang->load('info',$this->session->userdata("lang"));
		}
		
		//加载网站设置
		$this->load->model("Agent_model");
		$this->WEB_SET=$this->Agent_model->load_websetinfo("39d9eacc-a08f-11e0-8075-fa0426affde8");
	
		$this->WEB_Language = $this->session->userdata("lang");
		
		$this->user_data["user_account"] = "guest";
		$this->user_data["user_name"] = "guest";
		$this->user_data["vbook"]="1";
		
		$this->user_data["AgentCode"] = $this->session->userdata('WEB_AgentCode');
		$this->user_data["AgentName"] = $this->session->userdata('WEB_AgentName');
		$this->user_data["mainCurrency"] = $this->session->userdata('WEB_mainCurrency');
		$this->user_data["isrebate"] 	= $this->session->userdata('isrebate');
	
		//记录日志
		$this->load->model("Userlog_model");
		$this->Userlog_model->write_userlog();

     }
	public function index1()
	{
		$data = array();
	   	$data['css'][0] = '<link rel="stylesheet" type="text/css" href="'.site_url('css/validator.css').'" />';
	   	$data['title'] = $this->WEB_SET["WEB_WebsiteTitle_".lang("curlang")].
	   				$this->WEB_SET["WEB_WelcomeText_".lang("curlang")]."-".lang("home_title");
		
		
	   	//设置航空公司下拉菜单
	   	$airoption =lang("aircompany");
	   	$airlist['']='=='.lang("home_allairline").'==';
	   	foreach ($airoption as $airid => $airname)
	   	{
	   		$airlist[$airid]=$airid.'-'.$airname;
	   	}

	   	$data['selairline']=form_dropdown('airline', $airlist, '','style="width:164px"');

	   	//语言文件
	   	$data['Find_a_Flight']	=lang("Find_a_Flight");
	   	$data['home_Flight_Type']=lang('home_Flight_Type');
		$data['home_From']       =lang('home_From');
		$data['home_To']         =lang('home_To');
		$data['home_Depart']     =lang('home_Depart');
		$data['home_Return']     =lang('home_Return');
		$data['home_Airline']    =lang('home_Airline');
		$data['home_Find']       =lang('home_Find');
		$data['home_oneway']     =lang('home_oneway');
		$data['home_roundtrip']  =lang('home_roundtrip');
		$data['home_Find']		=lang('home_Find');
		
		if($this->input->get("directurl")!="")
		{
			$data["src_url"]=$this->input->get("directurl");
		}else
		{
			$data["src_url"]='fatair/search';
		}
		$this->load->view('fatair/BookTravelFlyGuam',$data);
	}

	public function index()
	{
		$data = array();
	   	$data['css'][0] = '<link rel="stylesheet" type="text/css" href="'.site_url('css/validator.css').'" />';
	   	$data['title'] = $this->WEB_SET["WEB_WebsiteTitle_".lang("curlang")].
	   				$this->WEB_SET["WEB_WelcomeText_".lang("curlang")]."-".lang("home_title");
		

	   	//设置航空公司下拉菜单
	   	$airoption =lang("aircompany");
	   	$airlist['']='=='.lang("home_allairline").'==';
	   	foreach ($airoption as $airid => $airname)
	   	{
	   		$airlist[$airid]=$airid.'-'.$airname;
	   	}

	   	$data['selairline']=form_dropdown('airline', $airlist, '','style="width:164px"');

	   	//语言文件
	   	$data['Find_a_Flight']	=lang("Find_a_Flight");
	   	$data['home_Flight_Type']=lang('home_Flight_Type');
		$data['home_From']       =lang('home_From');
		$data['home_To']         =lang('home_To');
		$data['home_Depart']     =lang('home_Depart');
		$data['home_Return']     =lang('home_Return');
		$data['home_Airline']    =lang('home_Airline');
		$data['home_Find']       =lang('home_Find');
		$data['home_oneway']     ='&nbsp;'.lang('home_oneway');
		$data['home_roundtrip']  ='&nbsp;'.lang('home_roundtrip');
		$data['home_Find']		=lang('home_Find');

		$this->load->view('fatair/search',$data);
	}

	//航班查询
	public function flight()
	{
		$this->load->library('encrypt');
		if(!$_POST)
		{
			redirect(base_url());
		}
		$data = array();
	   	$data['css'][0] = '<link rel="stylesheet" type="text/css" href="'.site_url('css/validator.css').'" />';
	   	$data['title'] = $this->WEB_SET["WEB_WebsiteTitle_".lang("curlang")].
	   				$this->WEB_SET["WEB_WelcomeText_".lang("curlang")].'-select flight';

	   	//设置左边选中框的默认值
	   	$data['sFromcitycode']	=$this->input->post("FromCityCode");
	   	$data['sDestcitycode']	=$this->input->post("DestCityCode");
	   	$data['sDepartDate']	=$this->input->post("DepartDate");
	   	$data['sReturnDate']	=$this->input->post("ReturnDate");
		
		
		
	   	
		$data["isback"]		=$this->input->post("isback");	//是否回程
		$data["goFlightData"]	=$this->input->post("goFlightData"); //去程航班
		
		$this->load->model("Air_model");
		
		//去程航班信息设置
		if($data["goFlightData"]!="")
		{
			
			$argoflight=$this->Air_model->getarrbookinfo($this->encrypt->decode($data["goFlightData"]));
	   		$data['argoflight']=$argoflight[0];
	   		$data["mainCurrency"]	=$this->session->userdata("WEB_mainCurrency");
		}
		
		$data['sFromcityname']	=$this->Air_model->getAirportname_buffer($data['sFromcitycode']);
	   	$data['sDestcityname']	=$this->Air_model->getAirportname_buffer($data['sDestcitycode']);
	   	
	   	//设置航空公司下拉菜单
	   	$airoption =lang("aircompany");
	   	$airlist['']='=='.lang("home_allairline").'==';

	   	foreach ($airoption as $airid => $airname)
	   	{
	   		$airlist[$airid]=$airid.'-'.$airname;
	   	}

	   	$data['selairline']=form_dropdown('airline', $airlist, '','style="width:164px"');

		$this->load->view('fatair/flight',$data);
	}

	//订单预订
	public function bookflight()
	{
		$this->load->library('encrypt');
		$data = array();
	   	$data['css'][0] = '<link rel="stylesheet" type="text/css" href="'.site_url('css/validator.css').'" />';
	   	$data['title'] = $this->WEB_SET["WEB_WebsiteTitle_".lang("curlang")].
	   				$this->WEB_SET["WEB_WelcomeText_".lang("curlang")].'-book';
	   	
	   	//设置左边选中框的默认值
	   	$data['sDepartDate']	=$this->input->post("DepartDate");
	   	$data['sReturnDate']	=$this->input->post("ReturnDate");
	   	
		$data["isback"]		=$this->input->post("isback");	//是否回程
		$data["goFlightData"]	=$this->input->post("goflightdata");	//去程航班信息
		
	   	if($this->input->post("go_flightinfo")=="")
	   	{
	   		redirect("home");	
	   	}
	   	//设置航空公司下拉菜单
	   	$airoption =lang("aircompany");
	   	$airlist['']='==All airline==';
	   	foreach ($airoption as $airid => $airname)
	   	{
	   		$airlist[$airid]=$airid.'-'.$airname;
	   	}

	   	$data['selairline']=form_dropdown('airline', $airlist, '','style="width:164px"');
	   
	   	//设置预订信息
	   	$this->load->model('Air_model');
	   	$this->load->model('Policy_model');
	   	$data['flightdata']		=$this->input->post("go_flightinfo");
	   	$data['flightbackdata']	=$this->input->post("back_flightinfo");
	   	
	   	
	   	$arairinfo=$this->Air_model->getarrbookinfo($this->encrypt->decode($data['flightdata']),$this->encrypt->decode($data['flightbackdata']));	   	
	   	
	   	if(substr($arairinfo[0]["PFD"],0,3)=="PFI")
	   	{
	   		//替换燃油费
	   		$tmpdata=$this->encrypt->decode($data['flightdata']);
	   		//没有运价的时候算一次运价
	   		if(strpos($tmpdata,"|-1|")>-1)
	   		{
		   		$arTaxdata=array(
		   			"fromport" => $arairinfo[0]["fromport"],
		   			"destport" => $arairinfo[0]["destport"],
		   			"FlightNo" => $arairinfo[0]["FlightNo"],
		   			"flightdate" => date("Y-m-d",strtotime($arairinfo[0]["flightdate"])),
		   			"class"	=> $arairinfo[0]["class"][0][0]
		   		);
		   
		   		$arairinfo[0]["OilFee"]=$this->Air_model->ws_queryiTaxbyflight($arTaxdata);
		   		
		   		
		   		$tmpdata=str_replace("|-1|","|".$arairinfo[0]['OilFee']."|",$tmpdata);
		   		$data['flightdata']		=$this->encrypt->encode($tmpdata);
		   		
		   		$tmpdata=$this->encrypt->decode($data['flightbackdata']);
		   		//echo $tmpdata;
		   		$tmpdata=str_replace("|-1|","|".$arairinfo[0]['OilFee']."|",$tmpdata);
		   	
		   		
		   		
		   		if($data['flightbackdata']!="")
		   		{
		   			$data['flightbackdata']	=$this->encrypt->encode($tmpdata);
		   			$arairinfo[1]["OilFee"]=$arairinfo[0]["OilFee"];
		   		}
	   		}
	   		
	   	}
	   	$data['arairinfo']=$arairinfo;
	   	
	   	$data["TGQNote"]=$this->Policy_model->get_TGQNotebyID($arairinfo[0]["PFDID"]);
	   	$data['sFromcitycode']	=$arairinfo[0]['fromport'];
	   	$data['sDestcitycode']	=$arairinfo[0]['destport'];
	   	
	   	$data["mainCurrency"]	=$this->user_data["mainCurrency"];
	   	
	   	//设置证件类型
	   	//设置航空公司下拉菜单
	   	$idType =lang("idType");

	   	$data['selCTType']=form_dropdown('CTType[]',$idType, '','id="sel_CTType_REPLACE_INDEX" style="width:154px"');
	   	
	   	
	   	//设置默认联系方式
	   	//$this->load->model("User_model");
	   	//$linkinfo=$this->User_model->get_customer($this->user_data["user_account"]);
	   	
	   	//$data["txLinkMan"]=$linkinfo["username"];
	   	//$data["txmobile"]=$linkinfo["mobilephone"];
	   	//$data["txmobile2"]=$linkinfo["tel1"];
	   	//$data["txEmail"]=$linkinfo["Email"];
		$data["txLinkMan"]="";
	   	$data["txmobile"]="";
	   	$data["txmobile2"]="";
	   	$data["txEmail"]="";
	   	
		$this->load->view('fatair/bookflight',$data);
	
	}

	//创建订单
	public function createorder()
	{
		//print_r($_POST);
		
		$this->load->model("Order_model");
		
		$this->user_data["user_account"] = $this->input->post("Email");
		$this->user_data["user_name"] =  $this->input->post("LinkMan");
		
		
		$bookresult = $this->Order_model->create_airOrder($_POST,$this->user_data);
    		//echo '<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>';
    		if($bookresult[0])
    		{
    			$url="airorder/$bookresult[1]";
			echo "<SCRIPT LANGUAGE=\"JavaScript\">";
			echo "alert(\"booking success. Your order number:$bookresult[1]\");";
			echo "window.location='$url';";
			echo "</SCRIPT>";  
    		}else
    		{
    			
    			echo "booking fail";
    		}
		
		$cookie = array(
		'name'   => 'fatair_email',
		'value'  => $_POST["Email"],
		'expire' => 86500
		  );
		
		set_cookie($cookie);
		
		$cookie = array(
		'name'   => 'fatair_LinkMan',
		'value'  => $_POST["LinkMan"],
		'expire' => 86500
		  );
		
		set_cookie($cookie);
	}
	
	//查看订单
	public function airorder($sOrderno)
	{
		$data=array();
		
		$data['css'][0] = "";
		$data['title'] = $this->WEB_SET["WEB_WebsiteTitle_".lang("curlang")].
	   				$this->WEB_SET["WEB_WelcomeText_".lang("curlang")].'-myorder';
		
				
		$this->load->model("Order_model");
		$bookdata= $this->Order_model->get_airOrder($sOrderno,$this->user_data);
		
		$this->load->model('Air_model');
		$flightdata=explode("^",$bookdata["order"]->Flightinfo1."^");
	   	$data['arairinfo']=$this->Air_model->getarrbookinfo($flightdata[0],$flightdata[1]);
	   	
	   	$data['sOrderno']=$sOrderno;
	   	$data['sFromcity']=$bookdata["order"]->fromportName;
	   	$data['sDestcity']=$bookdata["order"]->destportName;
	   	$data['sDepartDate']=date("Y-m-d",strtotime($bookdata["order"]->departdate));
	   	$data['sReturnDate']=date("Y-m-d",strtotime($bookdata["order"]->returndate));
	   	
	   	if($data['sReturnDate']=="1970-01-01")
	   	{
	   		$data['sReturnDate']="";
	   	}
		
		$data['TGQNote']=$bookdata["order"]->TGQNote;
		$data['LinkMan']=$bookdata["order"]->Linkman;
	   	$data['Mobile1']=$bookdata["order"]->LinkMobile1;
	   	$data['Mobile2']=$bookdata["order"]->LinkMobile2;
	   	$data['Email']	=$bookdata["order"]->LinkEmail;
	   	$data['mainCurrency']=$bookdata["order"]->mainCurrency;
	   	
	   	$data['cpsdata']=$bookdata["passlist"];
	   	
	   	$data['TotalPrice']=$bookdata["order"]->TotalPrice;
	   	$data['TotalPassenger']=$bookdata["order"]->TotalPassenger;
	   	
	   	$arOrderStatus=lang("OrderStatus");
	   	$data['OrderStatusDesc'] =$arOrderStatus[$bookdata["order"]->Status];
	   	$data['orderstatus']	=$bookdata["order"]->Status;
	   	$data['loglist']=$this->Order_model->getOrderlog($sOrderno,"公开日志");
	   	
	   	$data['aridType'] =lang("idType");
		$data['arPassType']=lang("PassType");
		$data['arGender']	=lang("Gender");
		
		
		$this->load->view("fatair/airorder",$data);
	}
	
	//支付
	public function dopayorder()
	{
		$data=array();
	   	$data['css'][0] ="";
	   	$data['title'] = $this->WEB_SET["WEB_WebsiteTitle_".lang("curlang")].
	   				$this->WEB_SET["WEB_WelcomeText_".lang("curlang")].'-queryorder';
	   	
	   	
		$data["orderNo"]=$this->input->post("Orderno");
		
		$this->load->model("Order_model");
		
		
		$bookdata=$this->Order_model->get_airOrder($data["orderNo"],$this->user_data);
		
		if($bookdata["order"]->Status!="0")		
		{
			exit("The wrong order status");
		}
		
		
		
		$data["amount"]=$bookdata["order"]->TotalPrice;
		$this->load->view("fatair/payOrder",$data);
	}
	
	//查询订单
	public function queryorder()
	{
		$data = array();
	   	$data['css'][0] ="";
	   	$data['title'] = $this->WEB_SET["WEB_WebsiteTitle_".lang("curlang")].
	   				$this->WEB_SET["WEB_WelcomeText_".lang("curlang")].'-queryorder';
	   	
	   	$data['sOrderno']		=$this->input->post("OrderNo");
	   	$data['sPassengerName']	=$this->input->post("PassengerName");
	   	$data['sEmail']		=$this->input->post("Email");
	   	$data['sLinkMan']		=$this->input->post("LinkMan");
	   	
	   	$data["bookdate1"]=date("Y-m-d");
	   	$this->load->model('Order_model');
	   	
		$this->load->helper('cookie');
		
	   	if (!$_POST)
	   	{
	   		$data['OrderStatus'] =lang("OrderStatus");
	   		$data["myorder"]=array();
	   		
	   		$data['sEmail']=get_cookie('fatair_email');
	   		$data['sLinkMan']=get_cookie('fatair_LinkMan');;
	   	}else
	   	{	   		
	   		$result= $this->Order_model->query_airOrder($_POST,$this->user_data["user_account"]);
	   		$data['OrderStatus'] =lang("OrderStatus");
	   		$data["myorder"]=$result;
	   		
	   		$cookie = array(
		     'name'   => 'fatair_email',
		     'value'  => $_POST["Email"],
		     'expire' => 86500
			  );
			
			set_cookie($cookie);
	   	}
	   	
	   	
	   	
	   	
		$this->load->view("fatair/queryorder",$data);	
	}
	public function admin()
	{
		$data=array();
		$data['error_msg']=$this->uri->segment(4);
     	$this->load->view("fatair/login",$data);
     
		
	}
	//支付成功
	public function paysuccess()
	{
		
		
		$this->load->model("Order_model");
		$this->Order_model->write_orderlog("PY".$this->input->post("x_description"),"支付通知","test".implode("//",$_POST),"system");
		
		
		$payresult="";
		
		//支付成功
		if($this->input->post("x_response_code")=="1" && 
			$this->input->post("x_response_reason_code")=="1"
		){
			$updatedata["Orderno"]=$this->input->post("x_description");
			$updatedata["status"]="1";
			$adminuser["uadmin_account"]="fatair";
			$adminuser["uadmin_name"]="fatair";
			$this->Order_model->update_orderstatus($updatedata,$adminuser);
			$payresult="pay success";
			
			//发送邮件
			$smessage=$data["orderNo"]."have paid";
			$this->Order_model->sendNotifyEmail($smessage,"Hello,".$smessage,$this->user_data["AgentCode"]);
		
					
		}else
		{
			$payresult="pay fail";			
		}
		
		$this->Order_model->write_orderlog($this->input->post("x_description"),"Payment notice",$payresult,"system");
		$url=site_url("fatair/airorder/".$updatedata["Orderno"]);
		echo "<SCRIPT LANGUAGE=\"JavaScript\">";
		echo "alert(\"$payresult\");";
		echo "window.location='$url';";
		echo "</SCRIPT>";  			
	}
	
	public function getTGQNote()
	{
		$this->load->model("Policy_model");
		$PFDID=$this->input->get("airline");
		$TGQNote=$this->Policy_model->get_TGQNotebyID($PFDID);
		
		echo $TGQNote;
		
		
	}
	


}

	

/* End of file home.php */
/* Location: ./application/controllers/home.php */